<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="商户名称" prop="merchantName">
        <el-input
          v-model="queryParams.merchantName"
          placeholder="请输入商户名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="人员姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入收货人/退货人姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="手机号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['business:merchantAddress:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:merchantAddress:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:merchantAddress:remove']"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="Download"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['business:merchantAddress:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="merchantAddressList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商户名称" align="center" prop="merchantName" />

      <el-table-column prop="type" label="地址类型" width="80">
        <template #default="scope">
          <span :style="scope.row.type == 'take' ? 'color: green;' : 'color: grey;'">
            {{ scope.row.type == 'take' ? '收货地址' : '退货地址' }}
          </span>
        </template>
      </el-table-column>

      <el-table-column label="收货人姓名" align="center" prop="name" />
      <el-table-column label="收货人手机号" align="center" prop="phone" />

      <el-table-column label="省/市/区" align="center" prop="provinceName">
        <template #default="scope">
          {{ scope.row.provinceName}}{{ scope.row.cityName}}{{ scope.row.regionName}}
        </template>
      </el-table-column>
      <el-table-column label="详细地址" align="center" prop="detailAddress" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" @click="handleInfo(scope.row)" >详情</el-button>
          <el-button link type="primary" @click="handleUpdate(scope.row)" v-hasPermi="['business:merchantAddress:edit']">修改</el-button>
          <el-button link type="primary" @click="handleDelete(scope.row)" v-hasPermi="['business:merchantAddress:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商户信息-地址对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="merchantAddressRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入收货人/退货人姓名" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入收货人/退货人手机号" />
        </el-form-item>

        <el-form-item label="类型">
          <el-radio-group v-model="form.type">
            <el-radio label="take">收货地址</el-radio>
            <el-radio label="return">退货地址</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="省" prop="provinceName">
          <template #default="scope">
            <el-select v-model="form.provinceName" size="small" style="width: 100%; margin-right: 5px" filterable clearable @change="provinceChange(form.provinceName)">
              <el-option v-for="item in provinceArr" :key="item.areaName" :label="item.areaName" :value="item.areaName" />
            </el-select>
          </template>
        </el-form-item>
        <el-form-item label="市" prop="cityName">
          <template #default="scope">
            <el-select v-model="form.cityName" size="small" style="width: 100%; margin-right: 5px" filterable clearable @change="cityChange(form.cityName)">
              <el-option v-for="item in cityArr" :key="item.areaName" :label="item.areaName" :value="item.areaName" />
            </el-select>
          </template>
        </el-form-item>
        <el-form-item label="区" prop="regionName">
          <template #default="scope">
            <el-select v-model="form.regionName" size="small" style="width: 100%;" filterable clearable @change="regionChange(form.regionName)">
              <el-option v-for="item in regionArr" :key="item.areaName" :label="item.areaName" :value="item.areaName" />
            </el-select>
          </template>
        </el-form-item>
        <el-form-item label="详细地址" prop="detailAddress">
          <el-input v-model="form.detailAddress" placeholder="请输入详细地址" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 详情 -->
    <el-dialog :title="title" v-model="infoOpen" width="700px" append-to-body>
      <el-form ref="merchantAddressRef" :model="form" :rules="rules" label-width="150px" class="many-box">
        <el-row>
          <el-col :span="24" class="title">地址信息</el-col>
          <el-col :span="12">
            <el-form-item label="收货人姓名: " prop="name">
              <span >{{ form.name }}</span>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="省：" prop="provinceName">
              <span >{{ form.provinceName }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="收货人手机号: " prop="phone">
              <span >{{ form.phone }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="市：" prop="cityName">
              <span >{{ form.cityName }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="详细地址：" prop="detailAddress">
              <span >{{ form.detailAddress }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="区：" prop="regionName">
              <span >{{ form.regionName }}</span>
            </el-form-item>
          </el-col>

        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>

<script setup name="MerchantAddress">
import { listMerchantAddress, getMerchantAddress, delMerchantAddress, addMerchantAddress, updateMerchantAddress,
  getCommonAreaList} from "@/api/business/merchantAddress";

const { proxy } = getCurrentInstance();

const merchantAddressList = ref([]);
const open = ref(false);
const infoOpen = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const provinceArr = ref([]);//省list
const cityArr = ref([]);//市list
const regionArr = ref([]);//区list

const data = reactive({
  form: {
    provinceId: null,
    cityId: null,
    regionId: null
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    merchantId: null,
    userId: null,
    type: null,
    name: null,
    phone: null,
    merchantName:null,
    provinceName: null,
    cityName: null,
    regionName: null,
    detailAddress: null,
    provinceId: null,
    cityId: null,
    regionId: null,
  },
  rules: {

  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询商户信息-地址列表 */
function getList() {
  loading.value = true;
  listMerchantAddress(queryParams.value).then(response => {
    merchantAddressList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  infoOpen.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    merchantId: null,
    userId: null,
    type: null,
    name: null,
    phone: null,
    provinceName: null,
    cityName: null,
    regionName: null,
    detailAddress: null,
    provinceId: null,
    cityId: null,
    regionId: null,
    delFlag: null,
    createTime: null,
    createBy: null,
    updateTime: null,
    updateBy: null
  };
  proxy.resetForm("merchantAddressRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加商户信息-地址";

  getProvinceList(0);
}

/** 详情操作 */
function handleInfo(row) {
  reset();
  const _id = row.id || ids.value
  getMerchantAddress(_id).then(response => {
    form.value = response.data;
    infoOpen.value = true;
    title.value = "商户地址详情";
  });


}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getMerchantAddress(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改商户信息-地址";
  });
  getProvinceList(0);
  getCityList(row.provinceId);
  getRegionList(row.cityId);
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["merchantAddressRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateMerchantAddress(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addMerchantAddress(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除该地址？').then(function() {
    return delMerchantAddress(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('business/admin/merchantAddress/export', {
    ...queryParams.value
  }, `商户地址_${new Date().getTime()}.xlsx`)
}



//获取省份
function getProvinceList(parentId){
  getCommonAreaList(parentId).then((response) => {
        provinceArr.value = response.data;
      })
      .catch(() => {})
}

//获取市
function getCityList(parentId){
  getCommonAreaList(parentId)
      .then((response) => {
        cityArr.value = response.data;
      })
      .catch(() => {})
}

//获取区
function getRegionList(parentId){
  getCommonAreaList(parentId)
      .then((response) => {
        regionArr.value = response.data;
      })
      .catch(() => {})
}

function provinceChange (provinceName) {
  cityArr.value = [];
  regionArr.value =  [];
  for (let i = 0; i < provinceArr.value.length; i++) {
    if (provinceArr.value[i].areaName === provinceName) {
      form.provinceName = provinceArr.value[i].areaName;
      form.value.provinceId = provinceArr.value[i].areaId;

      getCityList(provinceArr.value[i].areaId);
    }
  }
}


function cityChange (cityName) {
  regionArr.value = [];
  for (let i = 0; i < cityArr.value.length; i++) {
    if (cityArr.value[i].areaName === cityName) {
      form.cityName = cityArr.value[i].areaName;
      form.value.cityId = cityArr.value[i].areaId;
      getRegionList(cityArr.value[i].areaId);
    }
  }
}

function regionChange (regionName) {
  for (let i = 0; i < regionArr.value.length; i++) {
    if (regionArr.value[i].areaName === regionName) {
      form.regionName = regionArr.value[i].areaName;
      form.value.regionId = regionArr.value[i].areaId;
    }
  }
}

getList();
</script>

<style lang="scss" scoped>
.many-box {
  padding: 30px;

  .title {
    font-size: 16px;
    font-weight: bold;
    margin: 10px 0;

    &::before {
      content: "";
      display: inline-block;
      width: 4px;
      height: 20px;
      background-color: #409eff;
      margin-right: 10px;
      vertical-align: middle;
    }
  }
}
</style>
